using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Kopain
{

    /// <summary>This class is a helper class for making backups.
    /// The same code could go into the main form but this modulizing seems more easy to read.
    /// </summary>
    internal class Backup
    {

        /// <summary>This method erases old backup files and returns the name of the new one.
        /// </summary>
        /// <param name="savefile">The name of the savefile including path.</param>
        /// <returns>The name of the backup file to use.  It might exist already.</returns>
        internal static string EraseOld( string savefile )
        {
            string savefilePath = Path.GetDirectoryName( savefile );
            string savefileWOsuffix = Path.GetFileNameWithoutExtension( savefile );

            //  Use the backupTemplate too loop through all backed up files.
            //  The backup files are in the same dir as the savefile.
            //  We search for all files and use regex to filter out the old backup files.  This is more exact than using
            //  the file system's built in wild cards.
            
            //  Retrieve all files in the same directory as the savefile.  Zero or more of them might be backup files for
            //  this savefile.
            string[] allFiles = Directory.GetFiles( savefilePath, "*", SearchOption.TopDirectoryOnly );
            
            //  Use regular expression to filter out the old backup files.
            //
            string[] allBackupFiles = LIUS.Templates.Array.ArrayHelper.KeepByFunction<string>(
                allFiles,
                delegate( string filename )
                {
                    string regexExpression = string.Format( KopainSetting.Instance.Settings.BackupMatch, savefileWOsuffix );
                    System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex( regexExpression );
                    return regex.IsMatch( filename );
                }
                );

            //TODO:Sort.  They seem sorted now though.  Maybe no need to sort them?

            //  Create a list to hold the names of the files to delete.  The old backup files.
            List<string> backupFilesToDelete = new List<string>();
            int numOfBackupFilesToKeep = KopainSetting.Instance.Settings.BackupFilesToKeep;

            //  Loop through, but not all.  We want to keep X old backups.
            //
            for( int i = 0 ; i < allBackupFiles.Length - numOfBackupFilesToKeep ; i++ )
            {
                backupFilesToDelete.Add( allBackupFiles[i] );
            }

            //  Loop and delete.
            //
            foreach( string fileToDelete in backupFilesToDelete ){
                File.Delete( fileToDelete );
            }

            //  Return the name of the backup file to use this very moment.
            //
            return string.Format( KopainSetting.Instance.Settings.BackupTemplate, savefileWOsuffix, DateTime.Today.ToString( "yyyyMMdd" ) );
        }

    }

}
